const express = require('express');
const router = express.Router();

const project_handler = require('../router_handler/project');

const expressJoi = require('@escook/express-joi');
const {
	addProject_schema,
	updateProject_schema,
	deleteProject_schema,
	getProject_schema,
	getProjectName_schema,
	getProjectBody_schema,
	getProjectList_schema,
	deleteProjectList_schema,
	delete_project_image_schema,
	project_first_draft_schema,
	project_rewrite_schema,
	project_end_schema,
	project_fail_schema,
	delete_project_file_schema
} = require('../schema/project');

const multer = require('multer');
const uploadImage = multer({
	limits: {
		// 限制文件大小100M
		fileSize: 100000*1000,
		// 限制文件数量
		fils: 1
	},
	// 限制文件类型
	fileFilter: function(req, file, cb){
		if(file.mimetype == 'image/gif' || file.mimetype == 'image/png' || file.mimetype == 'image/jpeg'){
			cb(null, true)
		} else {
			cb(null, false)
		}
	},
	dest: 'static/img/'
});

// 添加项目
router.post('/addProject', expressJoi(addProject_schema), project_handler.addProject);
// 编辑项目
router.post('/updateProject', expressJoi(updateProject_schema), project_handler.updateProject);
// 删除项目
router.post('/deleteProject', expressJoi(deleteProject_schema), project_handler.deleteProject);
// 日历项目查询
router.get('/getProject', project_handler.getProject);
// 输入项目名称查询,模糊搜索
router.get('/getProjectName', expressJoi(getProjectName_schema), project_handler.getProjectName);
// 查询项目详情
router.get('/getProjectBody', project_handler.getProjectBody);
// 分页查询项目列表
router.get('/getProjectList', project_handler.getProjectList);
// 批量删除项目
router.post('/deleteProjectList', expressJoi(deleteProjectList_schema), project_handler.deleteProjectList);
// 状态进度查询项目列表
router.get('/stateProjectList', project_handler.stateProjectList)

// 文件断点续传
// 获取已上传文件的接口
router.get('/projectUploadAlready', project_handler.projectUploadAlready);
// 上传切片
router.post('/projectUploadChunk', project_handler.projectUploadChunk);
// 合并切片 (少了将路径存入数据库)
router.post('/projectUploadMerge', project_handler.projectUploadMerge);

// 多图上传
router.post('/projectUploadSingle',uploadImage.single('project_image'), project_handler.projectUploadSingle);
// 批量删除图片
router.post('/deleteProjectImage', expressJoi(delete_project_image_schema), project_handler.deleteProjectImage);
// 初稿提交
router.post('/projectFirstDraft', expressJoi(project_first_draft_schema), project_handler.projectFirstDraft);
// 改稿延长
router.post('/projectRewrite', expressJoi(project_rewrite_schema), project_handler.projectRewrite);
// 项目完结
router.post('/projectEnd', expressJoi(project_end_schema), project_handler.projectEnd);
// 项目烂尾
router.post('/projectFail', expressJoi(project_fail_schema), project_handler.projectFail);

// 删除项目文件
router.post('/deleteProjectFile', expressJoi(delete_project_file_schema), project_handler.deleteProjectFile);

module.exports = router;